<?php
class hrd_dinas_model extends MY_Model {
	var $nama_tabel = 'hrd_dinas ';
	
	function GetJSON() {
		$ls_sql	= "	SELECT	b.nama_karyawan, a.id_dinas_pk, a.id_karyawan_fk, a.jml_hari, a.ket_dinas, to_char(a.tgl_tiba, 'dd/mm/yyyy') tgl_tiba, to_char(a.tgl_mulai, 'dd/mm/yyyy') tgl_mulai, to_char(a.tgl_selesai, 'dd/mm/yyyy') tgl_selesai, a.ket_perpanjangan_dinas, a.tujuan, a.jenis_kendaraan, a.no_pol_kendaraan, a.uang_makan, a.uang_harian, a.penginapan, a.bahan_bakar, a.tol, a.biaya_lain, a.id_cost_center_fk, a.pemberi_tugas, a.id_pemberi_tugas
					FROM	".$this->nama_tabel." a
					LEFT JOIN hrd_karyawan b ON a.id_karyawan_fk = b.id_karyawan_pk
					WHERE a.tgl_mulai >= to_date('".post('tgl_awal')."', 'dd/mm/yyyy')
					AND a.tgl_mulai <= to_date('".post('tgl_akhir')."', 'dd/mm/yyyy')
					AND a.id_karyawan_fk like '%".post('id_karyawan_cari')."%'
					ORDER BY ".(post('sort')=='' ? 'id_dinas_pk' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
					
		return json_encode( $this->paging($ls_sql) );
					// print_r($ls_sql);
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_dinas_pk' => $arr_rule['id_dinas_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GenerateId() {
		$sql = "select * 
				from ( 
					select * 
					from hrd_dinas 
					order by id_dinas_pk desc
					)
				where ROWNUM <= 1
				";
		$data = $this->db->query($sql)->row_array();

		if (count($data) == 0) {
			return 1;
		} else {
			return $data['id_dinas_pk']+1;
		}
	}

}
?>